namespace SimpleWebServices.Models
{
    using System;
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.Text;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;
    using SimpleWebServices.Web;

    [XmlRoot("stylesheet")]
    public sealed class StyleSheet : IXmlSerializable
    {
        public StyleSheet()
        {
            this.Imports = new Collection<string>();
        }

        public string Conditional
        {
            get;
            set;
        }

        public string Href
        {
            get;
            set;
        }

        public string Media
        {
            get;
            set;
        }

        public Collection<string> Imports
        {
            get;
            private set;
        }

        XmlSchema IXmlSerializable.GetSchema()
        {
            throw new NotSupportedException();
        }

        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            if (null == reader)
            {
                throw new ArgumentNullException("reader");
            }

            this.Conditional = reader.GetAttribute("if");
            this.Href = reader.GetAttribute("href");
            this.Media = reader.GetAttribute("media");

            if (reader.IsEmptyElement)
            {
                reader.Read();
            }
            else
            {
                string name = reader.Name;
                while (reader.Read())
                {
                    if (XmlNodeType.EndElement == reader.NodeType && reader.Name == name)
                    {
                        reader.Read();
                        break;
                    }
                    else if ("import".Equals(reader.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        this.Imports.Add(reader.ReadString());
                    }
                }
            }
        }

        void IXmlSerializable.WriteXml(XmlWriter writer)
        {
            if (null == writer)
            {
                throw new ArgumentNullException("writer");
            }

            if (null != this.Conditional)
            {
                writer.WriteAttributeString("if", this.Conditional);
            }

            if (null != this.Href)
            {
                writer.WriteAttributeString("href", this.Href);
            }

            if (null != this.Media)
            {
                writer.WriteAttributeString("media", this.Media);
            }

            foreach (string import in this.Imports)
            {
                writer.WriteStartElement("import");
                writer.WriteString(import);
                writer.WriteEndElement();
            }
        }

        public override string ToString()
        {
            StringBuilder buffer = new StringBuilder();
            foreach (string import in this.Imports)
            {
                buffer.AppendLine(string.Format(CultureInfo.InvariantCulture, "@import \"{0}\";", Http.ToCurrentProtocol(import)));
            }

            return buffer.ToString();
        }
    }
}